package com.dy.design_pattern_study.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-03-13
 * @Content 这个是针对日期时间的使用
 */
public class testTimeUser {

    public static void main(String[] args) throws ParseException {
        //Date 转换成 LocalDateTime
        date2LocalDateTime();

        //LocalDateTime 转换成 Date
        //localDateTime2Date();

        //Dat
        // eTimeFormatter 字符串转换 LocalDateTime
        //transformDate();

        //Calendar 的使用 -线程不安全
        //CalendarUse();

    }

    /**
     * Date 转换成 LocalDateTime
     */
    private static void date2LocalDateTime() {
        Instant instant = new Date().toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime orderTime = LocalDateTime.ofInstant(instant, zone);
        System.out.println("orderTime = " + orderTime);
        System.out.println("orderTime = " + orderTime.getYear());
        System.out.println("orderTime = " + orderTime.getMonthValue());
        System.out.println("orderTime = " + orderTime.getDayOfMonth());
    }

    /**
     * LocalDateTime 转换成 Date
     */
    private static void localDateTime2Date() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.UK);
        LocalDateTime time = LocalDate.parse("2022-10-11", dtf).atStartOfDay();
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = time.atZone(zone).toInstant();
        Date date = Date.from(instant);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        System.out.println("format = " + format);
    }

    /**
     * DateTimeFormatter 字符串转换 LocalDateTime
     */
    private static void transformDate() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.UK);
        //TemporalAccessor time = dtf.parse("2022-10-11");
        LocalDateTime time = LocalDate.parse("2022-10-11", dtf).atStartOfDay();

        System.out.println("before year = " + time.getYear());
        System.out.println("before month = " + time.getMonthValue());
        System.out.println("before day = " + time.getDayOfMonth());

        System.out.println("------------分割线-----------");
        LocalDateTime afterTime = time.minusMonths(24);//得到前3月的时间
        System.out.println("after year = " + afterTime.getYear());
        System.out.println("after month = " + afterTime.getMonthValue());
        System.out.println("after day = " + afterTime.getDayOfMonth());
    }

    private static void CalendarUse() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dNow = new Date();   //当前时间
        Date dBefore;
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(sdf.parse("2021-11-01"));//把当前时间赋给日历
        System.out.println("calendar year = " + calendar.get(Calendar.YEAR));
        System.out.println("calendar month = " + (calendar.get(Calendar.MONTH) + 1));
        System.out.println("calendar day = " + calendar.get(Calendar.DAY_OF_MONTH));

        System.out.println("------------分割线-----------");
        calendar.add(Calendar.MONTH, -24);  //设置为前13个月
        dBefore = calendar.getTime();   //得到前3月的时间
        System.out.println("calendar year = " + calendar.get(Calendar.YEAR));
        System.out.println("calendar month = " + calendar.get(Calendar.MONTH));
        System.out.println("calendar day = " + calendar.get(Calendar.DAY_OF_MONTH));
    }
}
